<template>
  <div>
    <el-row>
      <el-col :span="12">
        <el-card style="margin: 5px;">
          <div class="title">采购待办</div>
          <div>
            <el-row>
              <el-col :span="8" style="text-align: center;cursor: pointer;">
                <div style="font-size: 25px; font-weight: bold; pointer;color:#FF0000"
                  @click="gotopage('buytodo.unpay')">{{buytodo.unpay}}</div>
                <div style="padding: 10px;">待支付</div>
              </el-col>
              <el-col :span="8" style="text-align: center;">
                <div style="font-size: 25px; font-weight: bold; cursor: pointer;color:#FF0000"
                  @click="gotopage('buytodo.unsend')">{{buytodo.unsend}}</div>
                <div style="padding: 10px;">待发货</div>
              </el-col>
              <el-col :span="8" style="text-align: center;">
                <div style="font-size: 25px; font-weight: bold; cursor: pointer;color:#FF0000"
                  @click="gotopage('buytodo.unticket')">{{buytodo.unticket}}</div>
                <div style="padding: 10px;">待收票</div>
              </el-col>
            </el-row>
          </div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card style="margin: 5px;">
          <div class="title">销售待办</div>
          <div>
            <el-row>
              <el-col :span="8" style="text-align: center;cursor: pointer;">
                <div style="font-size: 25px; font-weight: bold; pointer;color:#00aa00"
                  @click="gotopage('saletodo.unsend')">{{saletodo.unsend}}</div>
                <div style="padding: 10px;">待发货</div>
              </el-col>
              <el-col :span="8" style="text-align: center;cursor:pointer">
                <div style="font-size: 25px; font-weight: bold; pointer;color:#00aa00"
                  @click="gotopage('saletodo.unticket')">{{saletodo.unticket}}</div>
                <div style="padding: 10px;">待开票</div>
              </el-col>
              <el-col :span="8" style="text-align: center;cursor: pointer;">
                <div style="font-size: 25px; font-weight: bold; pointer;color:#00aa00"
                  @click="gotopage('saletodo.unincome')">{{saletodo.unincome}}</div>
                <div style="padding: 10px;">待回款</div>
              </el-col>
            </el-row>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-row>
      <el-col>
        <el-card style="margin: 5px;">
          <div style="display: flex;">
            <div class="title" style="flex: 1;">进销项发票汇总</div>
            <div style="">账套:
              <el-select placeholder="账套" v-model="account" style="width: 260px;" @change="handleSwitchAccount">
                <el-option v-for="(item,index) in accounts" :key="item.id" :label="item.name"
                  :value="item.name"></el-option></el-select>
            </div>
          </div>
          <div>
            <div id="tickettrends" style="width:100%;height:400px;border: 1px solid #f5f5f5; margin: 10px;"></div>
          </div>
        </el-card>

      </el-col>
    </el-row>

    <el-row>
      <el-col>
        <el-card style="margin: 5px;">
          <div class="title">销售订单趋势图</div>
          <div>
            <div id="trends" style="width:100%;height:300px;border: 1px solid #f5f5f5;"></div>
          </div>
        </el-card>
      </el-col>
    </el-row>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        buytodo: {
          unpay: 0,
          unsend: 0,
          unticket: 0
        },
        accounts: [],
        saletodo: {
          unsend: 0,
          unticket: 0,
          unincome: 0,
        },
        trends: {

        },
        tickettrends: {
          buyticket: [],
          saleticket: [],
          sbamount: []
        },
        account: '',
      }
    },
    mounted() {
      this.account = "河北鸿大智飞自动化有限公司";
      this.loadAccounts();
      this.loadData();
      this.loadTicketTrends();
    },
    methods: {
      loadAccounts() {
        this.axios.post("api/admin/account/list").then(res => {
          if (res.status == 200 && res.data.code == 0) {
            this.accounts = res.data.list;
          }
        })
      },
      handleSwitchAccount(val) {
        this.loadTicketTrends();
      },
      loadData() {
        this.axios.post("api/admin/dashboard/info").then(res => {
          if (res.status == 200 && res.data.code == 1) {
            this.buytodo = res.data.buytodo;
            this.saletodo = res.data.saletodo;
            this.trends = res.data.trends;
            this.updateJdTrendGraph();
          }
        });
      },
      gotopage(cmd) {
        switch (cmd) {
          case "buytodo.unpay":
            this.$router.push({
              path: "/admin/buypayment/list",
              query: {
                status: 0
              }
            });
            break;
          case "buytodo.unsend":
            this.$router.push({
              path: "/admin/buylist/list",
              query: {
                deliverstatus: 0
              }
            });
            break;
          case "buytodo.unticket":
            this.$router.push({
              path: "/admin/buylist/list",
              query: {
                buyticket_status: 0
              }
            });
            break;
          case "saletodo.unsend":
            this.$router.push({
              path: "/admin/orderdetail/index",
              query: {
                deliverstatus: 0
              }
            });
            break;
          case "saletodo.unticket":
            this.$router.push({
              path: "/admin/order/list",
              query: {
                saleticket_status: 0
              }
            });
            break;
          case "saletodo.unincome":
            this.$router.push({
              path: "/admin/order/list",
              query: {
                income_status: '1'
              }
            });
            break;

        }
      },
      loadTicketTrends() {
        const params = {
          account: this.account
        }
        this.axios.post("api/admin/dashboard/tickets", params).then(res => {
          if (res.status == 200 && res.data.code == 1) {
            this.tickettrends.saleticket = res.data.saleticket;
            this.tickettrends.buyticket = res.data.buyticket;
            this.tickettrends.sbticket = res.data.sbticket;
          }
          this.updateTicketTrendGraph();

        })
      },
      updateJdTrendGraph() {
        let myChart = this.$echarts.init(document.getElementById('trends'))
        myChart.setOption(this.trends);
        window.addEventListener("resize", () => { //监听浏览器窗口大小
          myChart.resize();
        });
      },
      updateTicketTrendGraph() {
        let saleChart = this.$echarts.init(document.getElementById('tickettrends'));
        saleChart.setOption({
          title: {
            text: "进销项票汇总",
            left: 'center'
          },
          tooltip: {
            "trigger": "axis",
            "axisPointer": {
              "type": "cross",
              "label": {
                "backgroundColor": "#6A7985",
              }
            }
          },
          grid: {
            "left": "3%",
            "right": "4%",
            "bottom": "3%",
            "containLabel": true
          },
          legend: {
            data: ['进项票额', "销项票额", "进销项差额"],
            icon: 'box',
            left: 'right'
          },
          yAxis: [{
            type: "value",
            name: "万元"
          }],
          series: [{
              name: "进项票",
              type: 'bar',
              barWidth: 20,
              data: this.tickettrends.buyticket,
              label: {
                show: true,
                position: 'top',
                textStyle: {
                  fontSize: '12px',
                }
              }
            },
            {
              name: "销项票",
              type: "bar",
              barWidth: 20,
              data: this.tickettrends.saleticket,
              label: {
                show: true,
                position: 'top',
                textStyle: {
                  fontSize: '12px',
                },
              }
            },
            {
              name: "差额",
              type: "bar",
              barWidth: 20,
              data: this.tickettrends.sbticket,
              label: {
                show: true,
                position: 'top',
                textStyle: {
                  fontSize: '12px',

                }
              }
            }
          ],

          xAxis: {
            "data": [
              "1月",
              "2月",
              "3月",
              "4月",
              "5月",
              "6月",
              "7月",
              "8月",
              "9月",
              "10月",
              "11月",
              "12月"
            ],
            "title": "月份"

          }
        });
        window.addEventListener("resize", () => { //监听浏览器窗口大小
          saleChart.resize();
        });
      }
    }
  }
</script>

<style>
</style>
